package com.tedu.day08springmvc.controller;

import com.tedu.day08springmvc.mapper.UserMapper;
import com.tedu.day08springmvc.pojo.dto.UserDTO;
import com.tedu.day08springmvc.pojo.entity.User;
import com.tedu.day08springmvc.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//@Controller
@RestController     //加入该注解后不用再单独添加@ResponseBody的注解了
public class UserController {
    @Autowired
    private UserMapper userMapper;


//    @RequestMapping("/users/reg")
    @PostMapping("/users/reg")
    //@ResponseBody
    public  String reg(UserDTO userDTO){
        User user = new User();
        System.out.println(user);
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        System.out.println(user);
        int num=userMapper.insertUser(user);





        return  num>0?"注册成功":"注册失败";
    }
    @R
//    @RequestMapping("/users/delete")
    @DeleteMapping("/users/delete/{id}")
   // @ResponseBody
    public  String delete(@PathVariable int id){
        int num=userMapper.deleteUser(id);
        return num>0?"删除成功":"删除失败";
    }
//    @RequestMapping("/users/update")
    @PutMapping("/users/update")
    //@ResponseBody
    public  String update(UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        int num=userMapper.updateUser(user);
        return num>0?"修改成功":"修改失败";
    }

//    @RequestMapping("/users/findAll")
    @GetMapping("/users/findAll")
   // @ResponseBody
    public List<UserVO> find(){
        List<UserVO> list=userMapper.findAll();
        list.forEach(System.out::println);
        return list;
    }

    @GetMapping("/users/findByid/{id}")
    public UserVO findById(@PathVariable int id){
        System.out.println(id);
        UserVO userVO=userMapper.findById(id);
        return userVO;
    }


}
